home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DOS.SWG / 0061_Error to file.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-24  |  6KB  |  192 lines

  1. {
  2. Here is a unit that I've played with a bit.. I have no idea who the original
  3. author is. What it does is expand the Runtime Errors reported by TP and
  4. optionally logs it to a file that you supply the name to.. It works fine for
  5. me on MSDOS 3.3 and 5.0.  If you make any improvements to it I would
  6. appreciate a copy of it..
  7. }
  8.  
  9. {$S-}
  10. UNIT Errors ;
  11.  
  12. INTERFACE
  13.  
  14. USES
  15.   Dos ;
  16.  
  17. VAR
  18.   ErrorFile  : PathStr ;                 { optional name you include in the }
  19.                                          { main program code                }
  20. PROCEDURE CheckRTError ;
  21.  
  22. IMPLEMENTATION
  23.  
  24. VAR
  25.   ErrorExitProc : Pointer ;
  26.  
  27. FUNCTION HexStr(w: Word): String ;
  28.   CONST
  29.     HexChars : Array [0..$F] of Char = '0123456789ABCDEF' ;
  30.   BEGIN
  31.     HexStr := HexChars[Hi(w) shr 4]
  32.             + HexChars[Hi(w) and $F]
  33.             + HexChars[Lo(w) shr 4]
  34.             + HexChars[Lo(w) and $F] ;
  35.   END ;
  36.  
  37. FUNCTION ExtendedError: String ; { goto DOS to get the last reported error }
  38.   VAR
  39.     Regs : Registers ;
  40.   BEGIN
  41.     FillChar(Regs,Sizeof(Regs),#0) ;
  42.     Regs.AH := $59 ;
  43.     MSDos(Regs) ;
  44.     CASE Regs.AX OF
  45.       $20 : ExtendedError := 'Share Violation' ;
  46.       $21 : ExtendedError := 'Lock Violation' ;
  47.       $23 : ExtendedError := 'FCB Unavailable' ;
  48.       $24 : ExtendedError := 'Sharing Buffer Overflow' ;
  49.       ELSE  ExtendedError := 'Extended Error ' + HexStr(Regs.AX) ;
  50.     END ; { case }
  51.   END ;
  52.  
  53. FUNCTION ErrorMsg(Err : Integer): String ;
  54. BEGIN
  55.   CASE Err OF
  56.       1 : ErrorMsg := 'Invalid Function Number';
  57.       2 : ErrorMsg := 'File Not Found';
  58.       3 : ErrorMsg := 'Path Not Found';
  59.       4 : ErrorMsg := 'Too Many Open Files';
  60.       5 : ErrorMsg := 'File Access Denied';
  61.       6 : ErrorMsg := 'Invalid File Handle';
  62.  
  63.      12 : ErrorMsg := 'Invalid File Access Code';
  64.  
  65.      15 : ErrorMsg := 'Invalid Drive Number';
  66.      16 : ErrorMsg := 'Cannot Remove Current Directory';
  67.      17 : ErrorMsg := 'Cannot Rename Across Drives';
  68.      18 : ErrorMsg := 'No More Files';
  69.  
  70.     100 : ErrorMsg := 'Disk Read Past End Of File';
  71.     101 : ErrorMsg := 'Disk Full';
  72.     102 : ErrorMsg := 'File Not Assigned';
  73.     103 : ErrorMsg := 'File Not Open';
  74.     104 : ErrorMsg := 'File Not Open For Input';
  75.     105 : ErrorMsg := 'File Not Open For Output';
  76.     106 : ErrorMsg := 'Invalid Numeric Format';
  77.  
  78.     150 : ErrorMsg := 'Disk is write protected';
  79.     151 : ErrorMsg := 'Unknown Unit';
  80.     152 : ErrorMsg := 'Drive Not Ready';
  81.     153 : ErrorMsg := 'Unknown command';
  82.     154 : ErrorMsg := 'CRC Error in data';
  83.     155 : ErrorMsg := 'Bad drive request structure length';
  84.     156 : ErrorMsg := 'Disk seek error';
  85.     157 : ErrorMsg := 'Unknown media type';
  86.     158 : ErrorMsg := 'Sector not found';
  87.     159 : ErrorMsg := 'Printer out of paper';
  88.     160 : ErrorMsg := 'Device write fault';
  89.     161 : ErrorMsg := 'Device read fault';
  90.     162 : ErrorMsg := 'Hardware failure';
  91.  
  92.     163 : ErrorMsg := ExtendedError ;
  93.  
  94.     200 : ErrorMsg := 'Division by zero';
  95.     201 : ErrorMsg := 'Range check error';
  96.     202 : ErrorMsg := 'Stack overflow error';
  97.     203 : ErrorMsg := 'Heap overflow error';
  98.     204 : ErrorMsg := 'Invalid pointer operation';
  99.     205 : ErrorMsg := 'Floating point overflow';
  100.     206 : ErrorMsg := 'Floating point underflow';
  101.     207 : ErrorMsg := 'Invalid floating point operation';
  102.     208 : ErrorMsg := 'Overlay manager not installed';
  103.     209 : ErrorMsg := 'Overlay file read error';
  104.     210 : ErrorMsg := 'Object not initialized';
  105.     211 : ErrorMsg := 'Call to abstract method';
  106.     212 : ErrorMsg := 'Stream registration error';
  107.     213 : ErrorMsg := 'Collection index out of range';
  108.     214 : ErrorMsg := 'Collection overflow error';
  109.     215 : ErrorMsg := 'Arithmetic overflow error';
  110.     216 : ErrorMsg := 'General protection fault';
  111.   END ;
  112. END ;
  113.  
  114. FUNCTION LZ(W : Word): String ;
  115.   VAR
  116.     s : String ;
  117.   BEGIN
  118.     Str(w:0,s) ;
  119.     IF Length(s) = 1 THEN s := '0' + s ;
  120.     LZ := s ;
  121.   END ;
  122.  
  123. FUNCTION TodayDate : String ;
  124.   VAR
  125.     Year,
  126.     Month,
  127.     Day,
  128.     Dummy,
  129.     Hour,
  130.     Minute,
  131.     Second : Word ;
  132.   BEGIN
  133.     GetDate(Year, Month, Day, Dummy) ;
  134.     GetTime(Hour, Minute, Second, Dummy) ;
  135.     TodayDate := LZ(Month) + '/' + LZ(Day) + '/' + LZ(Year-1900)
  136.                + '   ' + LZ(Hour) + ':' + LZ(Minute) ;
  137.   END ;
  138.  
  139. {$F+}
  140. PROCEDURE CheckRTError ;
  141.   VAR
  142.    F : Text ;
  143.   BEGIN
  144.     IF ErrorAddr <> Nil THEN
  145.       BEGIN
  146.         IF ErrorFile <> '' THEN
  147.           BEGIN
  148.             Assign(F,ErrorFile) ;
  149.             {$I-} Append(F) ; {$I+}
  150.             IF IOResult <> 0 THEN Rewrite(F) ;
  151.             Writeln(F,'Date: ' + TodayDate) ;
  152.             Write(F,'RunTime Error #',ExitCode,' at ') ;
  153.             Write(F,HexStr(Seg(ErrorAddr^)) + ':') ;
  154.             WriteLn(F,HexStr(Ofs(ErrorAddr^))) ;
  155.             Writeln(F,ErrorMsg(ExitCode)) ;
  156.             Writeln(F,'') ;
  157.             Close(F) ;
  158.           END ;
  159.         Writeln('Date: ' + TodayDate) ;
  160.         Write('RunTime Error #',ExitCode,' at ') ;
  161.         Write(HexStr(Seg(ErrorAddr^)) + ':') ;
  162.         WriteLn(HexStr(Ofs(ErrorAddr^))) ;
  163.         Writeln(ErrorMsg(ExitCode)) ;
  164.         Writeln ;
  165.         ErrorAddr := Nil ;          { reset variable so TP doesn't report  }
  166.         ExitProc := ErrorExitProc ; { the error and reset the Exit Pointer }
  167.       END ;
  168.   END ;
  169. {$F-}
  170.  
  171. BEGIN
  172.   ErrorFile := '' ;                 { don't log the error to a file }
  173.   ErrorExitProc := ExitProc ;
  174.   ExitProc := @CheckRTError ;
  175. END.
  176.  
  177. {============== DEMO  ==============}
  178.  
  179. PROGRAM Test ;
  180.  
  181. USES
  182.   Errors ;
  183.  
  184. VAR
  185.   TestFile : Text ;
  186.  
  187. BEGIN
  188.   ErrorFile := 'TESTERR.TXT' ;     { log errors to this file }
  189.   RunError(3) ;                    { test whatever you want  }
  190. END.
  191.  
  192.